<script setup>
import { ref } from 'vue'
import PageContainer from '@/components/PageContainer.vue'
import { PasswordRenewService } from '@/api/user.js'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/index.js'
import { useRouter } from 'vue-router'

const passwordForm = ref({
  old_pwd: '',
  new_pwd: '',
  repassword: '',
})

const formRef = ref()
const router = useRouter()

const rules = {
  old_pwd: [
    { required: true, message: '请输入旧密码', trigger: 'blur' },
    { pattern: /^\S{6,15}$/, message: '密码必须是6-15位非空字符', trigger: 'blur' },
  ],
  new_pwd: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { pattern: /^\S{6,15}$/, message: '密码必须是6-15位非空字符', trigger: 'blur' },
  ],
  re_pwd: [
    { required: true, message: '请再次输入新密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== passwordForm.value.new_pwd) {
          return callback(new Error('与新密码不一致'))
        } else {
          return callback()
        }
      },
      trigger: 'blur',
    },
  ],
}

const userStore = useUserStore()

const submitNewPassword = async () => {
  await formRef.value.validate()
  const res = await PasswordRenewService(passwordForm.value)
  console.log(res)
  if (res.data.code === 0) {
    ElMessage.success('修改成功，请重新登陆')
    await userStore.resetToken()
    await userStore.resetRememberInfo()
    await router.push('/login')
  }
  else {
    ElMessage.error(res.data.message)
  }
}

const resetNewPassword = () => {
  passwordForm.value = {
    old_pwd: '',
    new_pwd: '',
    repassword: '',
  }
}
</script>

<template>
  <page-container title="重置密码">
    <el-form
      ref="formRef"
      style="max-width: 600px"
      :model="passwordForm"
      :rules="rules"
      label-width="auto"
    >
      <el-form-item label="旧密码" prop="old_pwd">
        <el-input v-model="passwordForm.old_pwd" placeholder="请输入旧密码" type="password" />
      </el-form-item>
      <el-form-item label="新密码" prop="new_pwd">
        <el-input v-model="passwordForm.new_pwd" placeholder="请输入新密码" type="password" />
      </el-form-item>
      <el-form-item label="再次输入" prop="re_pwd">
        <el-input
          v-model="passwordForm.re_pwd"
          placeholder="请再次输入旧密码"
          type="password"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="submitNewPassword" type="primary">修改密码</el-button>
        <el-button @click="resetNewPassword">重置</el-button>
      </el-form-item>
    </el-form>
  </page-container>
</template>

<style scoped>
/* 表单容器样式 */
.page-container {
  display: flex;
  justify-content: center;
  padding: 20px;
}

/* 表单样式 */
.el-form {
  width: 100%;
  padding: 20px;
}

/* 表单标题样式 */
.page-container[title]::before {
  content: attr(title);
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 20px;
  display: block;
}

/* 按钮组样式 */
.el-form-item:last-child {
  margin-top: 30px;
  text-align: center;
}

/* 按钮间距 */
.el-button + .el-button {
  margin-left: 15px;
}

/* 输入框样式 */
.el-input {
  width: 100%;
}
</style>

